package com.dunqi.gpm.shunqing.home.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.CommonUtil;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.activity.ProjectProgressDetailsActivity;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/23 0023.
 */

public class ProjectDayReportFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArrayList<ProjectTimeline> data = new ArrayList<>();
    private DayReportAdapter adapter;
    private Long projectId;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

        net();

    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectProgressList(projectId, page)
                .compose(RxSchedulers.<Page<ProjectTimeline>>io_main())
                .subscribe(new Consumer<Page<ProjectTimeline>>() {
                    @Override
                    public void accept(Page<ProjectTimeline> pages) throws Exception {
                        ArrayList<ProjectTimeline> list = pages.getList();

                        if (page==1) {
                            data.clear();
                        }

                        data.addAll(list);

                        page++;
                        adapter.notifyDataSetChanged();
                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new DayReportAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);
        adapter.setEmptyView(new EmptyView(context));
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectProgressDetailsActivity.start((Activity) context, data.get(position).getProPlanProgressId());
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    class DayReportAdapter extends RecyclerAdapter<ProjectTimeline> {

        public DayReportAdapter(@Nullable List<ProjectTimeline> data) {
            super(R.layout.item_day_report, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ProjectTimeline item) {
            final CheckBox checkBox = helper.getView(R.id.expand_cb);
            final View container = helper.getView(R.id.container);

            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkBox.isChecked()) {

                        container.setVisibility(View.GONE);
                        checkBox.setBackgroundResource(R.mipmap.green_add);
                    }else {
                        container.setVisibility(View.VISIBLE);
                        checkBox.setBackgroundResource(R.mipmap.green_less);
                    }

                }
            });

            helper.setText(R.id.time_tv, item.getReportTime());
            NormalView reportNameView = helper.getView(R.id.report_name_tv);
            reportNameView.setText(item.getReporterName());

            NormalView reportContentView = helper.getView(R.id.report_content_tv);
            reportContentView.setText(item.getProgressDesc());

            TextView auditTv= helper.getView(R.id.status_tv);
            int auditStatus = item.getAuditStatus();
            String auditText;
            int color;
            switch (auditStatus) {
                case 0:
                    auditText = "创建";
                    color = CommonUtil.getColor(R.color.text_color_select_oragin);
                    break;
                case 1:
                    auditText = "待审核";
                    color = CommonUtil.getColor(R.color.text_color_select_oragin);
                    break;
                case 2:
                    auditText = "审核通过";
                    color = CommonUtil.getColor(R.color.text_color_normal_green);
                    break;
                case 3:
                    auditText = "审核未通过";
                    color = CommonUtil.getColor(R.color.red);
                    break;
                default:
                    auditText = "";
                    color = CommonUtil.getColor(R.color.text_color_normal_gray);
            }
            auditTv.setText(auditText);
            auditTv.setTextColor(color);
        }


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_day_report;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        projectId =  arguments.getLong(Constance.PROJECTID);
    }

    public static ProjectDayReportFragment newInstance(Long projectId) {
        ProjectDayReportFragment fragment = new ProjectDayReportFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECTID, projectId);
        fragment.setArguments(bundle);
        return fragment;
    }
}
